package com.sam;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Servlet implementation class MemberController
 */
public class MemberController extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private static final String LIST_MEMBER = "lst_member.jsp";
        private static final String ADD_MEMBER = "add_member.jsp";
        private static final String EDIT_MEMBER = "edit_member.jsp";
        private static final String DEL_MEMBER = "del_member.jsp";
        private static final String INDEX_PAGE = "index.jsp";
        private static final String LOGIN_PAGE = "login.jsp";

        private MemberDAO dao;
		private HttpSession session;
		private String username;
		private String userid;
		private String password;
		private String nickname;
		private String email;
		private String status;
		private String vid;
		
        /**
         * @see HttpServlet#HttpServlet()
         */
        public MemberController() {
                super();
                dao = new MemberDAO();
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
                String forward = "";
                String action = request.getParameter("action");
                
                userid = request.getParameter("userid");
        		password = request.getParameter("password");
        		username = request.getParameter("username");
        		nickname = request.getParameter("nickname");
        		email = request.getParameter("email");
        		status = request.getParameter("status");
        		vid = request.getParameter("vid");
        		
                System.out.println("action = " + action);
                System.out.println("forward = " + forward);
                
                session = request.getSession();
                                
                if (dao.login(userid, password)){
    				// Login Success
    				forward = LIST_MEMBER;
    			}else{
    				// Login Faile
    				forward = LOGIN_PAGE;
    				action = "";
    			}
    			
    			System.out.println("login = " + dao.login(userid, password));
    			                
                if (action.equalsIgnoreCase("lst_member")) {
                	forward = LIST_MEMBER;
                }
                
                if (action.equalsIgnoreCase("add_member")) {
                	if (request.getParameter("userid") != null) {
                		Member m = new Member(userid, username, nickname, password, email, status, vid);
                		int rowCount = dao.add(m);
                		if (rowCount > 0){
                			forward = LIST_MEMBER;
                		} else{
                			forward = ADD_MEMBER;
                		}
                	} else {
                		forward = ADD_MEMBER;
                    }
                }
                        
                if (action.equalsIgnoreCase("edit_member")) {
                	Member m = dao.getMemberById(request.getParameter("userid"));
                	request.setAttribute("m", m);
                	forward = EDIT_MEMBER;
                }
                        
                if (action.equalsIgnoreCase("del_member")) {
                	dao.delete(request.getParameter("userid"));
                	forward = LIST_MEMBER;
                }
                    
                List<Member> members = dao.getAll();
                request.setAttribute("members", members);
                System.out.println("forward = " + forward);
                
                session.setAttribute("center", forward);
                RequestDispatcher dispatcher = request
                                .getRequestDispatcher(INDEX_PAGE);
                dispatcher.forward(request, response);
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doPost(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
                String action = request.getParameter("action");
                if (action.equalsIgnoreCase("edit_member")) {
                        Member m = new Member(request.getParameter("userid"),
                        					  request.getParameter("username"),
                                        	  request.getParameter("nickname"),
                                        	  request.getParameter("password"),
                                        	  request.getParameter("email"));
                        dao.update(m);
                        List<Member> members = dao.getAll();
                        request.setAttribute("members", members);
                        session.setAttribute("center", LIST_MEMBER);
                        RequestDispatcher dispatcher = request
                                        .getRequestDispatcher(INDEX_PAGE);
                        dispatcher.forward(request, response);
                }
        }

}